package com.z_talk.client.view;

import com.z_talk.client.core.ClientSender;
import com.z_talk.client.localization.TalkLocalzation;
import com.z_talk.core.enums.RequestEnum;
import com.z_talk.core.message.RequestMessage;
import com.z_talk.core.model.UserInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TalkView {
    private JPanel talkPanel;
    private JTextArea talkConsole;
    private JTextArea talkSendMessage;
    private JButton sendBut;
    private JTextArea userinfoArea;
    private JList onlineUserList;

    UserInfo userInfo;
    UserInfo[] userModels;
    private static ClientSender clientSender;
    public Map<String,PrivateTalkView> privateTalkViewMap = new HashMap<>();

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public TalkView(UserInfo userInfo) {
        this.userInfo = userInfo;
        this.clientSender = ClientSender.getInstance();
        loadData();
        init();
        talkPanel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                super.componentShown(e);
                System.out.println(111);
            }
        });
    }

    private void loadData() {

    }

    private void init() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        JFrame jFrame = new JFrame("z_talk,自由的交流平台");
        jFrame.setContentPane(this.talkPanel);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(800,600);
        int x = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int y = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        jFrame.setLocation((x - jFrame.getWidth()) / 2, (y-jFrame.getHeight())/ 2);

        userinfoArea.append("登录用户："+userInfo.getNickName());
        userinfoArea.append("\n性别："+userInfo.getSex());
        userinfoArea.append("\n年龄："+userInfo.getAge());

        talkConsole.append(TalkLocalzation.loadWorldTalk(userInfo));

        talkSendMessage.requestFocus();

        onlineUserList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (e.getClickCount() == 2){
                    JList jList = (JList) e.getSource();
                    UserInfo user = userModels[jList.getSelectedIndex()];
                    privateTalkViewMap.put(user.getUserId(),new PrivateTalkView(userInfo,user));
                }
            }
        });

        sendBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String talkMsg = talkSendMessage.getText();
                RequestMessage message = new RequestMessage();
                message.setMessageType(RequestEnum.WORLD_TALK_REQUEST);
                Map<String,Object> attribute = new HashMap<>();
                attribute.put(RequestEnum.WORLD_TALK_REQUEST.getAttr(),talkMsg);
                message.setAttribute(attribute);
                try {
                    clientSender.sendMsg(message);
                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                }finally {
                    talkSendMessage.setText("");
                    talkSendMessage.requestFocus();
                }
            }
        });
    }

    // 更新消息显示
    public void updateMessage(String msg){
        talkConsole.append(msg);
    }

    // 更新在线人列表
    public void updateOnlineList(UserInfo[] userInfos){
        this.userModels = Arrays.stream(userInfos).filter(item -> {
            if (item.getNickName().equals(userInfo.getNickName())) {
                return false;
            }else {
                return true;
            }
        }).toArray(UserInfo[]::new);
        String[] users = Arrays.stream(this.userModels).map(item -> {
            return item.getNickName()+"("+item.getUserId()+")";
        }).toArray(String[]::new);
        onlineUserList.setListData(users);
    }

}
